package com.xcf.controller.business.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.util.ResponseResult;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.service.business.common.riverboat.RiverBoatManager;
import com.xcf.service.system.menu.MenuManager;
import com.xcf.service.system.role.RoleManager;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 类名称：RiverBoatManagerController
 * 创建人：Micro Chen
 * 更新时间：2017年11月07日
 */
@Controller
@RequestMapping(value = "/riverboat")
@SuppressWarnings("all")
public class RiverBoatManagerController extends BaseController {

    String menuUrl = "riverboat/listRiverBoat.do"; //菜单地址(权限用)
    @Resource(name = "riverBoatService")
    private RiverBoatManager riverBoatService;
    @Resource(name = "roleService")
    private RoleManager roleService;
    @Resource(name = "menuService")
    private MenuManager menuService;

    /**
     * 显示用户列表
     *
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listRiverBoat")
    public ModelAndView listRiverBoat(Page page) throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        String keywords = pd.getString("keywords");                //关键词检索条件
        if (null != keywords && !"".equals(keywords)) {
            pd.put("keywords", keywords.trim());
        }
        String lastLoginStart = pd.getString("lastLoginStart");    //开始时间
        String lastLoginEnd = pd.getString("lastLoginEnd");        //结束时间
        if (lastLoginStart != null && !"".equals(lastLoginStart)) {
            pd.put("lastLoginStart", lastLoginStart + " 00:00:00");
        }
        if (lastLoginEnd != null && !"".equals(lastLoginEnd)) {
            pd.put("lastLoginEnd", lastLoginEnd + " 00:00:00");
        }
        page.setPd(pd);
        List<PageData> riverboatList = riverBoatService.listRiverBoats(page);    //列出用户列表

        mv.setViewName("business/common/riverboat/riverboatmanager");
        mv.addObject("riverboatList", riverboatList);

        mv.addObject("pd", pd);

        return mv;
    }

    @RequestMapping(value = "/findRiverBoat")
    @ResponseBody
    public ResponseResult findRiverBoat(int page, int limit) throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();
        PageHelper.startPage(page,limit);
        List<Map> list = riverBoatService.search(pd);
        return ResponseResult.success(new PageInfo(list));



        /*
        public void findRiverBoat(HttpServletResponse response) throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();
        Page page = new Page();
        page.setPd(pd);
//		List<PageData> entityMainTain = entityMaintainService.findEntityMaintainInfo(pd);
        List<PageData> entityMainTain = riverBoatService.listRiverBoats(page);
        System.out.println("----------------------------------------------------");
        System.out.println(page.getCurrentResult()+"当前记录起始索引");
        System.out.println(page.getCurrentPage()+"当前页");
        System.out.println("----------------------------------------------------");

        JSONArray json = new JSONArray();
        for (PageData p : entityMainTain) {
            JSONObject jo = new JSONObject();
//			 	jo.put("id", p.getString("PARENT_DEPARTMENT_ID"));	  
//	            jo.put("text", p.getString("PARENT_DEPARTMENT_NAME"));
            jo.put("BOAT_ID", p.get("BOAT_ID"));
            jo.put("RIVER_BOAT_CODE", p.getString("RIVER_BOAT_CODE"));
            jo.put("RIVER_BOAT_NAME", p.getString("RIVER_BOAT_NAME"));
            jo.put("SETTLE_COMPANY", p.get("SETTLE_COMPANY"));
            jo.put("SETTLE_DEPARTMENT_SEARCH_CODE", p.getString("SETTLE_DEPARTMENT_SEARCH_CODE"));
            if (pd.getString("ENABLE") == "1") {
                jo.put("ENABLE", "是");
            } else {
                jo.put("ENABLE", "否");
            }


            json.add(jo);
        }

        response.setContentType("text/html; charset=utf-8");
      //  response.getWriter().write("{" + "\"" + "total" + "\"" + ":" + "\"" + "5" + "\"" + "," + "\"" + "root" + "\"" + ":" + json.toString() + "}");
        response.getWriter().write("{" + "\"" + "total" + "\"" + ":" + "\"" + page.getTotalResult() + "\"" + "," + "\"" + "root" + "\"" + ":" + json.toString() + "}");
        response.getWriter().flush();*/
    }


    /**
     * 保存江船
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveRiverBoat")
    public void saveRiverBoat(HttpServletRequest request, HttpServletResponse response) throws Exception {

        PageData pd = new PageData();
        pd = this.getPageData();
        JSONObject obj = new JSONObject();
        response.getWriter().write(obj.toString());

        response.getWriter().flush();

//      pd.getString("BOAT_ID").isEmpty()
        if (pd.getString("BOAT_ID")==null || pd.getString("BOAT_ID")=="") {
            pd.put("BOAT_ID", this.get32UUID());    //ID 主键
            pd.put("RIVER_BOAT_CODE", pd.getString("RIVER_BOAT_CODE"));
            pd.put("RIVER_BOAT_NAME", pd.getString("RIVER_BOAT_NAME"));                        //IP
            pd.put("SETTLE_COMPANY", pd.getString("SETTLE_COMPANY"));                    //状态
            pd.put("SETTLE_DEPARTMENT_SEARCH_CODE", pd.getString("SETTLE_DEPARTMENT_SEARCH_CODE"));
            pd.put("ENABLE", pd.getString("ENABLE"));

            if (null == riverBoatService.findByRiverBoatName(pd)) {    //判断用户名是否存在
                riverBoatService.saveRiverBoat(pd);                    //执行保存
                obj.put("msg", "success");
            } else {

                obj.put("msg", "failed");
            }
        } else {
            if (pd.getString("ENABLE").equals("是")) {
                pd.put("ENABLE", 1);
            } else {
                pd.put("ENABLE", 0);
            }

            riverBoatService.updateRiverBoatInfo(pd);
            obj.put("msg", "success");
        }
    }


    /**
     * 显示用户列表(弹窗选择用)
     *
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listRiverBoatsForWindow")
    public ModelAndView listRiverBoatsForWindow(Page page) throws Exception {
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        String keywords = pd.getString("keywords");                //关键词检索条件
        if (null != keywords && !"".equals(keywords)) {
            pd.put("keywords", keywords.trim());
        }
        String lastLoginStart = pd.getString("lastLoginStart");    //开始时间
        String lastLoginEnd = pd.getString("lastLoginEnd");        //结束时间
        if (lastLoginStart != null && !"".equals(lastLoginStart)) {
            pd.put("lastLoginStart", lastLoginStart + " 00:00:00");
        }
        if (lastLoginEnd != null && !"".equals(lastLoginEnd)) {
            pd.put("lastLoginEnd", lastLoginEnd + " 00:00:00");
        }


        page.setPd(pd);
        List<PageData> riverboatList = riverBoatService.listRiverBoats(page);    //列出用户列表

        mv.setViewName("business/common/riverboat/riverboatmanager");
        mv.addObject("riverboatList", riverboatList);

        mv.addObject("pd", pd);

        return mv;
    }

    /**
     * 删除提箱场站信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteInfo")
    @ResponseBody
    public String deleteInfo() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();

        String data = pd.getString("array");

        JSONArray items = JSONArray.fromObject(data);
        for (int i = 0, size = items.size(); i < size; i++) {
            JSONObject item = items.getJSONObject(i);
            String id = item.getString("id").trim();
            PageData pdq = new PageData();
            pdq.put("BOAT_ID", id);
            riverBoatService.deleteRiverBoatInfo(pdq);
        }
        return null;

    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
    }

}
